<?php


class champ_eventchoice
{
	var $event_id = 0;
	var $champ_id = 0;
	var $track_logo = '';
	var $race_id;
		
	var $select_results_options = '';
	
	function champ_eventchoice($champ_id, $event_id = 0)
	{
		$this->champ_id = $champ_id;
		$this->event_id = $event_id;
		if(isset($_GET['event']))
		{
			$this->event_id = $_GET['event'];
		}
		
		global $db, $lang;
		$q = 'SELECT DISTINCT e_id, t_fullTitle, e_title, c_title, t_logoThmb,   ' . // r_race, d_id,
  				'(SELECT ed_date FROM '. EVENTDATES_TABLE .' WHERE ed_event = e_id ORDER BY ed_date ASC LIMIT 0,1) as ed_date ' .
				'FROM ' . TRACKS_TABLE . ', ' . RACERESULTS_TABLE . ', ' 
					    . EVENTS_TABLE . ', ' . CHAMPIONSHIPS_TABLE .
				' WHERE e_track = t_id AND e_championship = c_id AND e_id = r_event AND c_id = ' . $champ_id .
				' ORDER BY ed_date ASC';
		
		$res = $db->sql_query($q);
		
		$matched = false;
		$t_event = 0;
		$t_track = 0;
		
		while ($row = $db->sql_fetchrow($res))
		{
			$this->records[] = $row;
			if($this->event_id == $row['e_id'])
			{
				$this->track_logo = $row['t_logoThmb'];
				$this->select_results_options .= '<option value="' . $row['e_id'] .'" SELECTED>' . 
													$row['e_title'] . ': ' . $row['t_fullTitle'] . '</option>';
				$matched = true;
			}
			else if($this->event_id == 0)
			{
				$this->event_id = $row['e_id'];
				$this->track_logo = $row['t_logoThmb'];
				$this->select_results_options .= '<option value="' . $row['e_id'] .'" SELECTED>' . 
													$row['e_title'] . ': ' . $row['t_fullTitle'] . '</option>';
				$matched = true;
			}
			else
			{
				$this->select_results_options .= '<option value="' . $row['e_id'] .'">' . 
													$row['e_title'] . ': ' . $row['t_fullTitle'] . '</option>';
				if($t_event == 0)
				{
					$t_event = $row['e_id'];
					$t_track = $row['t_logoThmb'];
				}
			}
		}
		
		if($matched == false)
		{
			$this->event_id = $t_event;
			$this->track_logo = $t_track;
		}
	}
	
	function getEventID()
	{
		return $this->event_id;
	}
	
	function getTrackLogo()
	{
		return $this->track_logo;
	}
	
	function render()
	{
		global $lang, $skin_dir;
		$id = $this->champ_id;
		$action = 'championships';
		$mode = 'incidents';
		$label = 'IncidentEvent';
		$select_results_options = $this->select_results_options;
		include $skin_dir . 'tpl/eventChoice.tpl';
	}
}


class champ_entrantsview
{
	var $id;
	var $entrants;
	var $numbers;
	var $divisions;
	var $display;

	function champ_entrantsview($option, $data)
	{
		global $lang;
		
		$this->display = $data['resno_count'] > 0 || $data['resdiv_count'] > 0 ? true : false;
		$this->id = $_GET['id'];
		$this->entrants = $option != 'entrants' ? '<a href="championships.php?mode=entrants&id=' . $this->id . '">' . 
							$lang['EntrantsCurrent'] . '</a>' : $lang['EntrantsCurrent'];
		$this->numbers = $data['resno_count'] > 0 ?
							($option != 'resnumbers' ? '<a href="championships.php?mode=resnumbers&id=' . $this->id . '">' . 
							$lang['EntrantsNumbers'] . '</a>' : $lang['EntrantsNumbers']) : '';
		$this->divisions = $data['resdiv_count'] > 0 ?
							($option != 'resdivisions' ? '<a href="championships.php?mode=resdivisions&id=' . $this->id . '">' . 
							$lang['EntrantsDivisions'] . '</a>' : $lang['EntrantsDivisions']) : '';
		
	}
	
	function render()
	{
		if($this->display)
		{
			global $lang, $skin_dir;
		
			$entrants = $this->entrants;
			$numbers = $this->numbers;
			$divisions = $this->divisions;
			
			include $skin_dir . 'tpl/champChooseEntrants.tpl';
		}		
	}
}


class champ_honour
{
	var $items = Array();

	var $winners;

	function champ_honour($data, $winners)
	{
		$this->winners = $winners;
			
		include_once('includes/page_header.php');
	
		
		$titleData = Array();
		
		if($data['c_title'] == $data['d_title'])
		{
			$title = $data['c_title'];
		}
		else
		{
			$title = $data['c_title'] . ' - ' . $data['d_title'];
		}
		
		if($data['hc_cat'] == 0)
		{
			$cat = 'drivers';
		}
		else if($data['hc_cat'] == 1)
		{
			$cat = 'teams';
		}
		else
		{
			$cat = 'manufacturers';
		}
		
		if($data['d_logoThmb'] == '')
		{
			$titleData['logo'] = $data['c_logoThmbURL'];
		}
		else
		{
			$titleData['logo'] = $data['d_logoThmb'];
		}
		
		$titleData['title'] = '<a href="championships.php?mode=tables&id=' . $data['c_id'] . '&division=' . $data['d_id'] . '&table=' . $cat .
						'">' . $data['hc_title']  . '</a>';
						
		$titleData['subTitle'] = '<a href="championships.php?mode=tables&id=' . $data['c_id'] . '&division=' . $data['d_id'] . '&table=' . $cat .
						'">' . $title  . '</a>';
						
		$this->items[] = new page_header($titleData, 'title', 'subTitle', 'logo', '');
		
	}
	
	function render()
	{		
		foreach($this->items as $item)
		{
			$item->render();
		}
		
		include_once('includes/renderInclude.php');
		
		global $skin_dir;
		
		$tbl = new renderInclude('tpl/tableStart.tpl');
		$tbl->render();
		
		foreach ($this->winners as $winner)
		{
			$title1 = $winner['i_title'];
			$title2 = $winner['j_title'];
			$title3 = $winner['k_title'];
			
			$trophy = '<img src="' . $skin_dir . 'images/trophy' . $winner['rank'] . '.jpg"/>';
			
			if($winner['imageThmb'] != '' && $winner['image'] != '')
				$image ='<a href="' . $winner['image'] . '" rel="lightbox[champimages]" title="' . 
					htmlentities($title1) . ' ' . htmlentities($title2) . '"><img src="' . $winner['imageThmb'] . '"/></a>';
			else if($winner['imageThmb'] != '')
				$image = '<img src="' . $winner['imageThmb'] . '"/>';
			else
				$image = '';
			
			include $skin_dir . 'tpl/honourItem.tpl';
		}
		
		$tbl = new renderInclude('tpl/tableEnd.tpl');
		$tbl->render();
		
		echo '<br><br>';
	}
}

class champ_details
{
	var $mode;
	var $data;
	
	var $items = Array();

	function champ_details($data, $mode)
	{
		$this->data = $data;
		$this->mode = $mode;
		
		if($mode == 'calendar')
		{
			$this->setupCalendar();
		}
		elseif($mode == 'cars')
		{
			$this->setupCars();
		}
		elseif($mode == 'entrants')
		{
			$this->setupEntrants();
		}
		elseif($mode == 'resnumbers')
		{
			$this->setupReservedNumbers();
		}
		elseif($mode == 'resdivisions')
		{
			$this->setupReservedDivisions();
		}
		elseif($mode == 'tables')
		{
			$this->setupTables();
		}
		elseif($mode == 'results')
		{
			$this->setupResults();
		}
		elseif($mode == 'honours')
		{
			$this->setupHonours();
		}
		elseif($mode == 'incidents')
		{
			$this->setupIncidents();
		}
		elseif($mode == 'history')
		{
			$this->setupHistory();
		}
	}
	
	function render()
	{
		if($this->mode == 'Mini')
		{
			$this->renderMini();
		}
		else if($this->mode == 'intro')
		{
			$this->renderIntroduction();
		}
		else
		{
			$this->renderItems();
		}
	}
	
	function renderMini()
	{
		global $lang, $skin_dir;
		$data = $this->data;
		$data['c_introText'] = formatOutput($data['c_introText']);
		
		if($data['c_logoURL'] == '')
		{
			$c_logo = '';
		}
		else
		{
			$c_logo = '<a href="championships.php?id=' . $data['c_id'] . '"><img src="' . $data['c_logoURL'] . '" alt=""></a>';
		}

                $signups = '';
                if($data['c_openSignUps'] == 1)
                {
                    //not logged in
                    if(! isset($_SESSION['valid_id']))
                        $signups = '<a href="index.php?action=login">' . sprintf($lang['LoginToEnter'], $lang['ChampSignupChampionship']) . '</a>';

                    //already entered
                    else if($_SESSION['valid_id'] == $data['champentered'])
                        $signups = '<a href="championships.php?mode=entrants&id=' . $data['c_id'] . '">' .
                                sprintf($lang['AlreadyEntered'], $lang['ChampSignupChampionship']) . '</a>';
                    
                    //incorrect license
                    else if($data['l_order'] < $data['userlicense'])
                        $signups = '<a class="closed" href="drivers.php?mode=license&id=' . $_SESSION['valid_id'] . '">' .
                                sprintf($lang['IncorrectLicense'], $lang['ChampSignupChampionship']) . '</a>';

                    //can enter
                    else
                        $signups = '<a class="open" href="championships.php?mode=entrants&id=' . $data['c_id'] . '">' .
                                sprintf($lang['EnterObject'], $lang['ChampSignupChampionship']) . '</a>';
                    

                }
                
                $calendar = '<a href="championships.php?mode=calendar&id=' . $data['c_id'] . '">' . $lang['calendar'] . '</a>';
                $results = '<a href="championships.php?mode=results&id=' . $data['c_id'] . '">' . $lang['EventResults'] . '</a>';
                $standings = '<a href="championships.php?mode=tables&id=' . $data['c_id'] . '">' . $lang['tables'] . '</a>';

                $game_image = '<img class="tableArticleImage" src="' . $data['g_logoThmb'] . '"/>';

		$license_image = '';
		if($data['l_imageThmb'] != '' && isset($_SESSION['valid_id']))
		{
		 	$license_image = '<a href="drivers.php?id=' . $_SESSION['valid_id'] . '&mode=license"><img class="tableArticleImage" src="' . $data['l_imageThmb'] . '"/></a>';
		}
		else if($data['l_imageThmb'] != '')
		{
		 	$license_image = '<img class="tableArticleImage" src="' . $data['l_imageThmb'] . '"/>';
		}
		
		$c_title = '<a href="championships.php?id=' . $data['c_id'] . '">' . $data['c_title'] . '</a>';
		include $skin_dir . 'tpl/champMini.tpl';
	}
	
	function renderItems()
	{
		foreach($this->items as $value)
		{
			$value->render();
		}
	}

	function renderIntroduction()
	{
		global $lang, $skin_dir;
		$data = $this->data;

                $c_logo = '';
		if($data['c_logoURL'] != '')
		{
			$c_logo = '<img src="' . $data['c_logoURL'] . '" alt="">';
		}

		$l_logo = '';
		if($data['l_imageThmb'] != '')
		{
			$l_logo = '<img src="' . $data['l_imageThmb'] . '" alt="">';
		}

                $g_logo = '';
		if($data['g_logoThmb'] != '')
		{
			$g_logo = '<img src="' . $data['g_logoThmb'] . '" alt="">';
		}

		
		$data['c_meetingText'] = formatOutput($data['c_meetingText']);
		$data['c_introText'] = formatOutput($data['c_introText']);
		$data['c_scoringText'] = formatOutput($data['c_scoringText']);
		$active = $data['c_active'] == 1 ?  $lang['ChampionshipActive'] : $lang['ChampionshipInactive'] ;
		$active_class = $data['c_active'] == 1 ? 'Open' : 'Closed' ;
		$signups = $data['c_openSignUps'] == 1 ? $lang['ChampionshipSignUpsOpen'] : $lang['ChampionshipSignUpsClosed'];
		$signups_class = $data['c_openSignUps'] == 1 ? 'Open' : 'Closed' ;
		
		$signups = '<a class="' . $signups_class. '" href="championships.php?mode=entrants&id=' . $_GET['id'] . '">' . $signups . '</a>';
		
		$champ_info = $lang['ChampionshipDivisions'] . ': ' . $data['c_divisions'] . '<br>' .
					  $lang['ChampionshipEventsPerMeeting'] . ': ' . $data['c_eventsPerMeeting'] . '<br>'.
					  $lang['ChampionshipFirstEvent'] . ': ' . $data['c_firstRaceF'] . '<br>' .
					  $lang['ChampionshipLastEvent'] . ': ' . $data['c_lastRaceF'] . '<br><br>';
					  
		$rules = ($data['c_rules'] == '' ? '' : formatOutput($data['c_rules']) . '<br><br>' ) . 
			($data['c_handbookURL'] == '' ?  '' : '<a href="' . $data['c_handbookURL'] . '">' . $lang['ChampionshipHandbookURL'] . '</a><br><br>') ;
		$rules .= $lang['ChampionshipRulesIntroduction'] . '<br><br>';
		$rules .= $data['l_id'] > 0 ? str_replace('{license}',$data['l_name'],$lang['ChampionshipLicenseInfo']) . '<br>':'';	
		$rules .= $data['c_carChanging'] == 1 ? $lang['ChampionshipCarChangingYes'] . '<br>' : $lang['ChampionshipCarChangingNo'] . '<br>';
		$rules .= $data['c_classChange'] == 1 ? $lang['ChampionshipClassChangingYes'] . '<br>' : $lang['ChampionshipClassChangingNo'] . '<br>';
		$rules .= $lang['ChampionshipTeamChangingOpt' .$data['c_teamChanging']] . '<br>';
		$rules .= $data['c_multipleCarsPerTeam'] == 1 ? $lang['ChampionshipOneMakePerTeamNo'] . 
							'<br>' : $lang['ChampionshipOneMakePerTeamYes'] . '<br>';
		$rules .= $data['c_driversPerTeam'] > 0 ? str_replace('{numdrivers}',$data['c_driversPerTeam'],$lang['ChampionshipDriversPerTeamYes']) . '<br>':'';	
		$rules .= $data['c_minRacesToEnter'] > 0 ? str_replace('{number}',$data['c_minRacesToEnter'],$lang['ChampionshipMinRacesToEnterInfo']) . '<br>':'';	
		$rules .= $data['c_multiDriverEntryMin'] > 0 ? sprintf($lang['ChampionshipMultiDriverInfo'], $data['c_multiDriverEntryMin'], $data['c_multiDriverEntryMax']) . '<br>' : '';
		$urls = 
				($data['c_broadcastLiveURL'] == '' ?  '' : '<a href="' . $data['c_broadcastLiveURL'] . '">' . $lang['ChampionshipBroadcastLiveURL'] . 
							'</a><br>') . 
				 ($data['c_broadcastDownloadURL'] == '' ?  '' : '<a href="' . $data['c_broadcastDownloadURL'] . '">' . 
				 			$lang['ChampionshipBroadcastDownloadURL'] . '</a><br>') . 
				 ($data['c_modDownloadURL'] == '' ?  '' : '<a href="' . $data['c_modDownloadURL'] . '">' . 
				 			$lang['ChampionshipModDownloadURL'] . '</a>');
		
		include $skin_dir . 'tpl/champIntro.tpl';
	}
	
	
	function setupCalendar()
	{
		global $lang, $db;
	
		include_once('includes/event_details.php');
		include_once('includes/addMessage.php');
		include_once('includes/renderInclude.php');
	
		$this->items[] = new addMessage($lang['CalendarText'], $this->data['c_title']);


                $userextra = "";
                if(isset($_SESSION['valid_id']) && is_numeric($_SESSION['valid_id']))
                {
                    $userextra = ',(SELECT cs_driver FROM '. CHAMPSIGNUPS_TABLE . '
                              WHERE cs_champ = c_id AND cs_driver = ' . $_SESSION['valid_id'] . ') as champentered,
                             (SELECT es_driver FROM '. EVENTSIGNUPS_TABLE . '
                              WHERE es_event = e_id AND es_driver = ' . $_SESSION['valid_id'] . ') as evententered,
                             (SELECT l_order FROM ' . LICENSES_TABLE . ', ' . LCUSERS_TABLE . '
                              WHERE u_license = l_id AND u_id = ' . $_SESSION['valid_id'] .') as userlicense';

                }

		$q = "SELECT *{$userextra},
                        (SELECT ed_date FROM " . EVENTDATES_TABLE . " WHERE ed_event = e_id ORDER BY ed_date ASC limit 0, 1) AS ed_date
                      FROM " . EVENTS_TABLE . ', ' . TRACKS_TABLE . ', ' . CHAMPIONSHIPS_TABLE . ", " . GAMES_TABLE . ", " . LICENSES_TABLE . "
                      WHERE e_track = t_id AND e_championship = c_id AND l_id = c_license AND c_game = g_id AND e_championship = " . $_GET['id'] . "
                      ORDER BY ed_date ASC";
		$res = $db->sql_query($q);
		$this->items[] = new renderInclude('tpl/tableStart.tpl');
		while ($row = $db->sql_fetchrow($res))
		{
			$this->items[] = new event_details($row, 'Mini');
		}
		$this->items[] = new renderInclude('tpl/tableEnd.tpl');
		
	}
	
	function setupResults()
	{
		global $lang;
		include_once('includes/page_header.php');
		include_once('includes/event_results.php');
		
		$choice = new champ_racechoice($this->data['c_id']);		
		$this->data['track_logo'] = $choice->getTrackLogo();
		
		$this->items[] = new page_header($this->data, 'c_title', '', 'c_logoThmbURL', 'track_logo', $lang['EventResults']);
		$this->items[] = $choice;

                if($this->data['c_stagedEvents'] == 0)
                {
                    $this->items[] = new event_results($choice->getEventID(), $choice->getDivisionID(), $choice->getRaceID(),
                                                'championships.php?mode=results&id=' . $this->data['c_id'] );
                }
                else
                {
                    $this->items[] = new rally_results($choice->getEventID(), $choice->getDivisionID(),
                                                'championships.php?mode=results&id=' . $this->data['c_id'] );
                }
	}
	
	function setupIncidents()
	{
		global $lang;
		include_once('includes/page_header.php');
		include_once('includes/incident_public.php');
		
		$choice = new champ_eventchoice($this->data['c_id']);		
		$this->data['track_logo'] = $choice->getTrackLogo();
		
		$this->items[] = new page_header($this->data, 'c_title', '', 'c_logoThmbURL', 'track_logo', $lang['Incidents']);
		$this->items[] = $choice;
		$this->items[] = new incident_public($choice->getEventID());
	}
	
	function setupEntrants()
	{
		global $lang, $db;
	
		include_once('includes/page_header.php');
		include_once('includes/addMessage.php');
		include_once('includes/renderInclude.php');
		include_once('includes/division_details.php');
		include_once('includes/championshipSignup.php');
	
		$this->items[] = new page_header($this->data, 'c_title', '', 'c_logoThmbURL', '', $lang['EntrantsCurrent']);
		$this->items[] = new champ_entrantsview('entrants', $this->data);
		$this->items[] = new championshipSignup($this->data);
		
		$q = 'SELECT * FROM ' . DIVISIONS_TABLE . ' WHERE d_champid = ' . $this->data['c_id'] . ' ORDER BY d_order';
		$res = $db->sql_query($q);
		while ($row = $db->sql_fetchrow($res))
		{
			$this->items[] = new division_details($row, 'entrants', $this->data['c_id'], $this->data['c_multiDriverEntryMin']);
		}
		
		$this->items[] = new division_details(0, 'entrants', $this->data['c_id'], $this->data['c_multiDriverEntryMin']);
		
			
	}
	
	function setupReservedNumbers()
	{
		global $lang, $db;
	
		include_once('includes/page_header.php');
		include_once('includes/champ_reserves.php');
		include_once('includes/addMessage.php');
	
		$this->items[] = new page_header($this->data, 'c_title', '', 'c_logoThmbURL', '', $lang['EntrantsNumbers']);
		$this->items[] = new champ_entrantsview('resnumbers', $this->data);
		$this->items[] = new addMessage($lang['EntrantsNumbersInfo'],'');
		$this->items[] = new reservednumbers($this->data['c_id']);
		
			
	}


	function setupHistory()
	{
		include_once('includes/seriesHistory.php');

                $type = 'drivers';
                if(isset($_GET['type']) && ($_GET['type'] == 'teams' || $_GET['type'] == 'manufacturers'))
                {
                    $type = $_GET['type'];
                }

		$this->items[] = new seriesHistory($type, $this->data);

	}
	
	function setupReservedDivisions()
	{
		global $lang, $db;
	
		include_once('includes/page_header.php');
		include_once('includes/champ_reserves.php');
		include_once('includes/addMessage.php');
	
		$this->items[] = new page_header($this->data, 'c_title', '', 'c_logoThmbURL', '', $lang['EntrantsDivisions']);
		$this->items[] = new champ_entrantsview('resdivisions', $this->data);
		$this->items[] = new addMessage($lang['EntrantsDivisionsInfo'],'');
		$this->items[] = new reserveddivisions($this->data['c_id']);
		
			
	}
	
	function setupTables()
	{
		global $lang, $db;
	
		include_once('includes/champ_table.php');
	
		if(! isset($_GET['division']) || ! is_numeric($_GET['division']))
		{
			$division = 0;
		}
		else
		{
			$division = $_GET['division'];
		}
		
		if(! isset($_GET['display']))
		{
			$display = 'overview';
		}
		else
		{
			$display = $_GET['display'];
		}
		
		if(! isset($_GET['table']) || ($_GET['table'] != 'drivers' && $_GET['table'] != 'teams'  && $_GET['table'] != 'manufacturers' ))
		{
			$table = 'drivers';
		}
		else
		{
			$table = $_GET['table'];
		}
		
	
		$this->items[] = new champ_table($this->data['c_id'], $division, $table, $display);
		
			
	}
	
	function setupCars()
	{
		global $lang, $db;
	
		include_once('includes/car_details.php');
		include_once('includes/addMessage.php');
		include_once('includes/renderInclude.php');
	
		$this->items[] = new addMessage($lang['CarsText'], $this->data['c_title']);
		
		$q = 'SELECT cl_title, cl_id FROM ' . CLASSES_TABLE . ', ' . CHAMP_CLASSES_TABLE . ' WHERE c_class = cl_id AND c_champid = ' . $_GET['id'];
		$res = $db->sql_query($q);
		while ($row = $db->sql_fetchrow($res))
		{
			$this->items[] = new addMessage('', $lang['CarClass']  . ': ' . $row['cl_title']);
			$this->items[] = new renderInclude('tpl/tableStart.tpl');
			$q = "SELECT * FROM " . CARS_TABLE . " WHERE c_class = " . $row['cl_id'] . ' ORDER BY c_manufacturer';
			$res2 = $db->sql_query($q);
			while ($row2 = $db->sql_fetchrow($res2))
			{
				$this->items[] = new car_details($row2, 'Mini');
			}
			$this->items[] = new renderInclude('tpl/tableEnd.tpl');
		}
		
	}
	
	
	function setupHonours()
	{
		global $db, $lang;
	
		include_once('includes/addMessage.php');
	
		$this->items[] = new addMessage('', $lang['ChampionshipHonours']);
		
		$q = 'SELECT * 
		      FROM ' . HONOURCATS_TABLE . ', ' . DIVISIONS_TABLE . ', ' . CHAMPIONSHIPS_TABLE . '  
			  WHERE hc_division = d_id AND d_champid = c_id AND c_id = ' . $_GET['id'];
		$res = $db->sql_query($q);
		while ($row = $db->sql_fetchrow($res))
		{
			if($row['hc_cat'] == 0)
			{
				$q = "Select CONCAT('<a href=\"drivers.php?id=', user_id, '\">', pf_drivername, '</a>')  as i_title, 
							 (SELECT CONCAT('<a href=\"teams.php?id=', t_id, '\">', t_name, '</a>') 
							 	FROM " . TEAMS_TABLE . " WHERE t_id = cs_team) as j_title,
							 CONCAT('<a href=\"cars.php?id=', c_id, '\">', c_manufacturer, ' ', c_model, '</a>') as k_title,
							 he_rank as rank,
							 cs_imageThmb as imageThmb,
							 cs_image as image
							 
							 
					  From ". HONOURENTRIES_TABLE . ", " . DRIVERNAME_TABLE . ", " . HONOURCATS_TABLE . ", " . 
					  		  CHAMPSIGNUPS_TABLE . ", " . CARS_TABLE . "
						  Where hc_id = he_cat AND he_item = user_id AND hc_id = " . $row['hc_id'] . " AND
								cs_driver = user_id AND cs_car = c_id AND cs_champ = " . $_GET['id'] . "
						  ORDER BY he_rank ASC";

			}
			else if($row['hc_cat'] == 1)
			{
				$q = "Select CONCAT('<a href=\"teams.php?id=', t_id, '\">', t_name, '</a>') as i_title, 
							 (SELECT group_concat(
							 		CONCAT('<a href=\"drivers.php?id=', user_id, '\">', pf_drivername, '</a>') SEPARATOR ', ') 
								 FROM " . DRIVERNAME_TABLE . ", " . CHAMPSIGNUPS_TABLE . "
								 WHERE cs_driver = user_id AND cs_team = t_id AND cs_div = hc_division) AS j_title,
							 (SELECT group_concat(DISTINCT
							 		CONCAT('<a href=\"cars.php?id=', c_id, '\">', c_manufacturer, ' ', c_model, '</a>') SEPARATOR ', ') 
								 FROM " . CARS_TABLE . ", " . CHAMPSIGNUPS_TABLE . "
								 WHERE cs_team = t_id  AND c_id = cs_car AND cs_div = hc_division GROUP BY c_id) AS k_title,
							 he_rank as rank,
							 t_logo as imageThmb,
							 '' as image
							 
							 
					  From ". HONOURENTRIES_TABLE . ", " . HONOURCATS_TABLE . ", " . TEAMS_TABLE . "
						  Where hc_id = he_cat AND he_item = t_id AND hc_id = " . $row['hc_id'] . "
						  ORDER BY he_rank ASC";

			}
			else if($row['hc_cat'] == 2)
			{
				$q = "Select CONCAT('<a href=\"cars.php?id=', c_id, '\">', c_manufacturer, ' ', c_model, '</a>') as i_title, 
							 '' AS j_title,
							 '' AS k_title,
							 he_rank as rank,
							 c_logoThmb as imageThmb,
							 c_logo as image
							 
					  From ". HONOURENTRIES_TABLE . ", " . HONOURCATS_TABLE . ", " . CARS_TABLE . "
						  Where hc_id = he_cat AND he_item = c_id AND hc_id = " . $row['hc_id'] . "
						  ORDER BY he_rank ASC";

			}
			
			$res2 = $db->sql_query($q);
			$winners = Array();
			while ($row2 = $db->sql_fetchrow($res2))
			{
				if($row['hc_cat'] == 0 && $row2['j_title'] == '')
				{
					$row2['j_title'] = $lang['Independent'];
				}
				$winners[] = $row2;
			}
			
			$this->items[] = new champ_honour($row, $winners);
		}
		
	}
}

?>